package school;

import java.util.List;
import java.util.LinkedList;

import school.course.Course;

public class School {
	private List<Instructor> instructors = new LinkedList<Instructor>();
	private List<Student> students = new LinkedList<Student>();
	private List<Course> courses = new LinkedList<Course>();

	private String name = "";

	public School(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public boolean addInstructor(Instructor instructor) {
		if (!instructors.contains(instructor)) {
			return instructors.add(instructor);
		}
		return true;
	}

	public boolean removeInstructor(Instructor instructor) {
		if (instructors.contains(instructor)) {
			if (removeStudent(instructor)) {
				return resetInstructor(instructor);
			}
			return false;
		}
		return true;
	}

	public boolean removeStudent(Student student) {
		if (students.contains(student)) {
			// remove student from current and future class
			for (Course course : student.getCurrentCourses()) {
				course.removeStudent(student);
			}
			for (Course course : student.getFutureCourses()) {
				course.removeStudent(student);
			}
			return true;
		}
		return true;
	}

	private boolean resetInstructor(Instructor instructor) {
		for (Course course : courses) {
			if (course.getInstructor() == instructor) {
				if (!course.setInstructor(null)) {
					return false;
				}
			}
		}
		return true;
	}

	public boolean registerStudent(Student student, Course course) {
		if (!addStudent(student)) {
			return false;
		}
		return student.addCourse(course);
	}

	protected boolean addStudent(Student student) {
		if (!students.contains(student)) {
			if (!students.add(student)) {
				return false;
			}
		}
		return true;
	}

}
